package com.ybkj.o2o.mc.controller.web;

import com.google.common.collect.Collections2;
import com.ybkj.o2o.mc.common.DataGrid;
import com.ybkj.o2o.mc.common.query.PassengerQuery;
import com.ybkj.o2o.mc.common.transport.Result;
import com.ybkj.o2o.mc.entity.Passenger;
import com.ybkj.o2o.mc.manager.PassengerManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shine on 15/8/19.
 */
@RestController
@RequestMapping("passenger")
public class PassengerController {

  @Autowired
  private PassengerManager passengerManager;

  /**
   * 分页查询所有乘客信息.
   *
   * @param param 模糊查询条件
   * @param page  页码
   * @param rows  每页显示数量
   */
  @RequestMapping(value = {"datagrid"}, method = RequestMethod.GET)
  @ResponseBody
  public DataGrid<Passenger> list(@RequestParam(required = false , value = "param") String param,
                                  @RequestParam("page") Integer page,
                                  @RequestParam("rows") Integer rows,
                                  @RequestParam(required = false, value = "appKey") String appKey
                                  ) {

    PassengerQuery passengerQuery = new PassengerQuery();
    passengerQuery.setKeyword(param);
    passengerQuery.setPage(page);
    passengerQuery.setRows(rows);
    passengerQuery.setAppKey(appKey);

    return passengerManager.findByPassengerQuery(passengerQuery);
  }


  @RequestMapping(value = "{id}", method = RequestMethod.GET)
  @ResponseBody
  public Passenger find(@PathVariable("id") Long id) {
    return passengerManager.fetch(id);
  }

  @RequestMapping(value = "updateChannel/{id}", method = RequestMethod.PUT)
  @ResponseBody
  public Map<String, Object> updateChannel(@PathVariable("id") Long id, @RequestParam("appKey") String appKey, @RequestParam("channelName") String channelName) {
    Map<String, Object> re = new HashMap<>();
    boolean success = false;
    try {
      int i = passengerManager.updateChannel(appKey, channelName, id);
      if (1 == i){
        success = true;
      }
    }catch (Exception e){
      e.printStackTrace();
    }
    re.put("success", success);
    return re;
  }

  @RequestMapping(value = "getWeixinInfos", method = RequestMethod.GET)
  @ResponseBody
  public Map<String, Object> getWeixinInfos(@RequestParam("ids")List<Long> ids) {
    Map<String, Object> re = new HashMap<>();
    boolean success = false;
    try {
      if(null != ids && 0 != ids.size()) {
        List<Map<String, Object>> infos = passengerManager.findWeixinInfoByIds(ids);
        if(null != infos && 0 != infos.size()){
          re.put("data",infos);
          success = true;
        }
      }

    }catch (Exception e){
      e.printStackTrace();
    }
    re.put("success", success);
    return re;
  }

}
